﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XSTDZ.ERP.Domain.Models
{
    /// <summary>
    /// 用户
    /// </summary>
   public partial class User
    {
       public User()
       {
           this.UserPermissions = new HashSet<UserPermission>();
           this.UserRoles = new HashSet<UserRole>();

           this.WorkGroups = new HashSet<WorkGroup>();
       }
       public User InitUser()
       {
           if (Guid.Equals(this.CreateID, Guid.Empty))
           {
               this.CreateID = this.Id;
           }
           if (this.Birthday == null || this.Birthday.Value <= DateTime.MinValue)
           {
               this.Birthday = DateTime.Parse("1900-01-01");
           }
           this.IsUse = true;
           this.CreateTime = DateTime.Now;
           this.Deleted = 0;
           //this.IsPiece = "0";

           return this;
       }
       ///// <summary>
       ///// 设置用户所属组织机构
       ///// </summary>
       ///// <param name="org">组织机构</param>
       ///// <returns>用户</returns>
       //public User SetOrg(Org org)
       //{
       //    if (this.Org == null || this.Org != org)
       //    {
       //        this.Org.Users.Remove(this);
       //        this.Org = org;
       //        org.Users.Add(this);
       //    }
       //    return this;
       //}
       ///// <summary>
       ///// 设置用户所属部门
       ///// </summary>
       ///// <param name="dept">部门</param>
       ///// <returns>用户</returns>
       //public User SetDept(Dept dept)
       //{
       //    if (this.Dept == null || this.Dept != dept)
       //    {
       //        this.Dept.Users.Remove(this);
       //        this.Dept = dept;
       //        dept.Users.Add(this);
       //    }
       //    return this;
       //} 
       /// <summary>
       /// 添加角色
       /// </summary>
       /// <param name="role">角色</param>
       /// <returns>用户</returns>
       //public User AddRole(Role role)
       //{
       //    this.UserPermission.AddRole(role);
       //    return this;
       //}

       /// <summary>
       /// 添加角色
       /// </summary>
       /// <param name="roles">角色集合</param>
       /// <returns>用户</returns>
       public User AddRoles(ICollection<Guid> roleIds)
       {
           if (roleIds != null)
           {
               foreach (var item in roleIds)
               {
                   if (!item.Equals(Guid.Empty))
                   {
                       this.UserRoles.Add(new UserRole
                       {
                           Id = Guid.NewGuid(),
                           RoleID = item,
                           UserID = this.Id
                       });
                   }
               }
           }
           return this;
       }
       /// <summary>
       /// 清空所有用户角色
       /// </summary>
       /// <returns></returns>
       public User ClearAllUserRole()
       {
           this.UserRoles.Clear();
           return this;
       }
      
       ///// <summary>
       ///// 重置角色
       ///// </summary>
       ///// <param name="roles">角色集合</param>
       ///// <returns>用户</returns>
       //public User ResetRoles(IEnumerable<Role> roles)
       //{
       //    this.UserPermission.ResetRoles(roles);
       //    return this;
       //}
       ///// <summary>
       ///// 添加权限
       ///// </summary>
       ///// <param name="permission">权限</param>
       ///// <returns>用户</returns>
       //public User AddPermission(Permission permission)
       //{
       //    this.UserPermission.AddPermission(permission);
       //    return this;
       //}
       ///// <summary>
       ///// 添加权限
       ///// </summary>
       ///// <param name="permissions">权限集合</param>
       ///// <returns>用户</returns>
       //public User AddPermissions(IEnumerable<Permission> permissions)
       //{
       //    this.UserPermission.AddPermissions(permissions);
       //    return this;
       //}
       /// <summary>
       /// 重置权限
       /// </summary>
       /// <param name="permissions">权限集合</param>
       /// <returns>用户</returns>
       //public User ResetPermissions(IEnumerable<Permission> permissions)
       //{
       //    this.UserPermission.ResetPermissions(permissions);
       //    return this;
       //}


    }
}
